package com.all.play.myapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.myapplication.Bean.ProductBean;
import com.all.play.myapplication.R;
import com.all.play.myapplication.view.DbPducShowDialog;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/23.
 * Time: 2018/4/23  9:23
 */

public class DatabaseManAdapter extends RecyclerView.Adapter<DatabaseManAdapter.MyDBViewHolder> {


    private Context context;
    private List<ProductBean> pbList;
    private DbPducShowDialog showDialog;

    public DatabaseManAdapter(Context context, List<ProductBean> pbList) {

        this.context = context;
        this.pbList = pbList;
    }

    @Override
    public MyDBViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.item_databse_manage, null);
        return new MyDBViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyDBViewHolder holder, final int position) {
        final ProductBean productBean = pbList.get(position);

        holder.itemView.setTag(position);

        holder.name.setText(productBean.getName());
        holder.prices.setText(productBean.getPrice()+"");
        holder.pages.setText(productBean.getPages()+"");
        holder.factory.setText(productBean.getFactory());

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //数据库移除数据的同时,需要将集合中对应的数据一并移除
                Log.i("deleteposition", "onClick: "+position);
                pbList.remove(pbList.get(position));
                DataSupport.deleteAll(ProductBean.class,"id=?",productBean.getId()+"");

                /*
                * 删除条目后不能在刷新当前条目,因为集合中-1,长度改变,当前position上的bean对象被remove,
                * notifyItemChanged(position),执行此方法,会崩溃
                * 应整体刷新
                * */
                //notifyItemChanged(position);
                notifyDataSetChanged();

            }
        });

        holder.setData(productBean);



    }

    @Override
    public int getItemCount() {
        return pbList==null?0:pbList.size();
    }

    class MyDBViewHolder extends RecyclerView.ViewHolder{

        TextView  name,prices,pages,factory,delete;

        public MyDBViewHolder(View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.tv_db_name);
            prices=itemView.findViewById(R.id.tv_db_price);
            pages=itemView.findViewById(R.id.tv_db_pages);
            factory=itemView.findViewById(R.id.tv_db_factory);
            delete=itemView.findViewById(R.id.tv_db_delete);



        }


        public void setData(final ProductBean productBean) {

            //点击条目跳转到详情页面
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });



            //长按修改内容
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Toast.makeText(context, "长按修改内容!", Toast.LENGTH_SHORT).show();
                    final int position = (int) view.getTag();
                    String name = productBean.getName();
                    double price = productBean.getPrice();
                    int pages = productBean.getPages();
                    String factory = productBean.getFactory();
                    //获取更改后的数据
                    //保存到本地
                    //条目数据刷新
                    //notifyDataSetChanged();
                    showDialog = new DbPducShowDialog((Activity) context,
                            R.style.show_shop_message_dialog,
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    switch (view.getId()) {
                                        case R.id.bt_change_db:
                                            //获取更改后的数据
                                            String change_name = showDialog.db_pduc_name.getText().toString();
                                            String change_price = showDialog.db_pdut_price.getText().toString();
                                            String change_pages = showDialog.db_pdut_page.getText().toString();
                                            String change_factory = showDialog.db_pdut_factory.getText().toString();
                                            Log.i("onLongClick", "onLongClick: " + change_name+"~~~"+change_price);
                                            //保存到数据库
                                            /*ProductBean pdbean = new ProductBean();
                                            pdbean.setPages(200);
                                            pdbean.updateAll("id=?","2");*/
                                            productBean.setName(change_name);
                                            productBean.setPages(Integer.valueOf(change_pages));
                                            productBean.setPrice(Double.valueOf(change_price));
                                            productBean.setFactory(change_factory);
                                            productBean.updateAll("id=?",""+productBean.getId());

                                            //条目数据刷新
                                            notifyItemChanged(position);
                                            //notifyDataSetChanged();
                                            showDialog.dismiss();
                                            break;
                                    }
                                }
                            },
                            name, price,
                            pages,factory);


                    showDialog.show();

                    return true;
                }
            });
        }
    }
}
